package com.jimet.user;

import java.sql.ResultSet;
import com.jimet.property.*;
import com.jimet.authorization.JimetAuthorization;
import com.jimet.authorization.JimetAuthorizations;
import com.jimet.db.JimetPropertyDB;
import com.jimet.db.JimetUserDB;
import com.jimet.server.JimetServerUser;
import com.jimet.group.*;


public class JimetUser extends JimetServerUser{

	private JimetGroups userGroups;
	private JimetUserProperties userProps;
	
	public JimetUser() {
		super();
	}

	public void load(long strUserId) throws Exception{
		this.load((int) strUserId);
	}
	
	public void load(int strUserId) throws Exception
	{
		JimetUserDB jimdb = new JimetUserDB();	
		ResultSet rs = jimdb.getUserByID(String.valueOf(strUserId));
		while(rs.next())
		{
			setUserId(rs.getInt(1));
			setUsername(rs.getString(2));
			setPasswordMD5(rs.getString(3));
			setCreateDate(rs.getTimestamp(4));
			setTimeStamped(rs.getBoolean(5));
			setTimeStampID(rs.getInt(6));
			this.fillUserAuthorizations();
			this.fillUserProps();

		}
		
		jimdb.disconnect();

	}
	
	
	public void fillUserProps() throws Exception
	{
		this.userProps = new JimetUserProperties();
		
		JimetPropertyDB jimdb = new JimetPropertyDB();
		ResultSet rs = jimdb.getObjProps(userProps.getTBL_PREFIX(),String.valueOf(this.getUserId()));
		
		while(rs.next())
		{	
			JimetUserProperty jproperty = new JimetUserProperty();
			jproperty.setName(rs.getString(1));
			jproperty.setValue(rs.getString(2));
			jproperty.setId(rs.getInt(3));

			this.userProps.add(jproperty);
		}  
		jimdb.disconnect();
	}

	
	public void removeUserRights() throws Exception
	{
		JimetUserDB jimdb = new JimetUserDB();
		jimdb.removeAuthorizationsAll(this.getUserId());
		jimdb.disconnect();
	}
	
	public JimetAuthorizations getUserAuthorizationsIn(String strInValues) throws Exception
	{
		JimetAuthorizations userAuths = new JimetAuthorizations();
		JimetUserDB jimdb = new JimetUserDB();
		ResultSet rs = jimdb.getUserAuthorizationsIn(this.getUserId(),strInValues);
		while(rs.next())
		{
			JimetAuthorization jauth = new JimetAuthorization();
			jauth.setId(rs.getInt(1));
			jauth.setShortCode(rs.getString(2));
			jauth.setDescription(rs.getString(3));
			userAuths.addAuth(jauth);
		}
		jimdb.disconnect();
		return userAuths;
	}
	
	public void setAuthChanges(JimetAuthorizations addedAuths,JimetAuthorizations removedAuths) throws Exception
	{
		JimetUserDB jimdb = new JimetUserDB();
		this.fillUserAuthorizationsDirectly();
		JimetAuthorizations auths = this.getJuserAuths();
		JimetAuthorizations cpyAddedAuths = new JimetAuthorizations();
		
		if(addedAuths!=null){
			addedAuths.reset();
			while(addedAuths.next()){
				cpyAddedAuths.addAuth(addedAuths.getNext());
			}
			while(auths.next()){
				if(cpyAddedAuths.contains(auths.getNext()))
					cpyAddedAuths.removeAuth(auths.getNext());
			}
			cpyAddedAuths.reset();
		}
		jimdb.setChangeAuthorizations(this.getUserId(),cpyAddedAuths,removedAuths);
		jimdb.disconnect();	
		
	}
	public boolean setAuthsAllSame(String strManagedIds, String [] selectedAuthIds) throws Exception
	{
		JimetUserDB jimdb = new JimetUserDB();
		try{
			jimdb.setAuthorizationsAll(this.getUserId(),strManagedIds,selectedAuthIds);
		}
		catch(Exception ex){ 
 		 ex.printStackTrace(); 
			System.out.println(ex.toString());
			return false;
		}
		
		jimdb.disconnect();
		return true;
		
	}
	
	public boolean setAuthsAllSame(String [] selectedAuthIds) throws Exception
	{
		JimetUserDB jimdb = new JimetUserDB();
		try{
			jimdb.setAuthorizationsAll(this.getUserId(),selectedAuthIds);
		}
		catch(Exception ex){ 
 		 ex.printStackTrace(); 
			System.out.println(ex.toString());
			return false;
		}
		
		jimdb.disconnect();
		return true;
		
	}
	
	public void addToGrpRelations(int groupId) throws Exception
	{ 
		JimetUserDB jimdb = new JimetUserDB();
		jimdb.addUserToGrpRelations(this.getUserId(),groupId);
		jimdb.disconnect();
	}
	
	public boolean isMemberOfGroup(int groupId) throws Exception
	{ 
		JimetUserDB jimdb = new JimetUserDB();
		boolean ret			= jimdb.isMemberOfGroup(this.getUserId(),groupId);
		jimdb.disconnect();
		return ret;
	}
	
	
	
	public void removeFromGroup(int groupId) throws Exception
	{ 
		JimetUserDB jimdb = new JimetUserDB();
		jimdb.removeUserFromGrp(this.getUserId(),groupId);
		jimdb.disconnect();
	}
	
	public boolean addAuths(String [] authIds) throws Exception{
		boolean result;
		JimetUserDB jimdb = new JimetUserDB();	
		result = jimdb.addUserAuthorizations(this.getUserId(),authIds);
		jimdb.disconnect();
		return result;
	}

	public boolean addAuth(int authId) throws Exception{
		boolean result;
		JimetUserDB jimdb = new JimetUserDB();	
		result = jimdb.addUserAuth(this.getUserId(),authId);
		jimdb.disconnect();
		return result;
	}

	public boolean removeAuth(int authId) throws Exception{
		boolean result;
		JimetUserDB jimdb = new JimetUserDB();	
		result = jimdb.removeUserAuth(this.getUserId(),authId);
		jimdb.disconnect();
		return result;
	}
	
	public int store() throws Exception
	{
		JimetUserDB jimdb = new JimetUserDB();
		int userId;
		if(this.userId == -1) 
			userId = jimdb.addUser(this.username,this.usernameAlt,this.passwordMD5);
		 else
			 userId = jimdb.updateUser(this.userId, this.username, this.usernameAlt , this.passwordMD5);
		jimdb.disconnect();
		
		return userId;
	}
	
	public boolean delete() throws Exception
	{
		JimetUserDB jimdb = new JimetUserDB();
		boolean queryOK;
			queryOK = jimdb.deleteUser(this.userId);
		jimdb.disconnect();
		return queryOK;
	}
	
	public String getProperty(String pname)
	{
		this.userProps.reset();
		while(this.userProps.next())
		{
			JimetProperty jprop = this.userProps.getNext();
			if(jprop.getName().equals(pname)) {
				return jprop.getValue();
			}
		}
		return "N/A";
	}



	public boolean hasProperty(String propName) throws Exception
	{
		
		while (this.userProps.next()) 
		{
			JimetProperty jprop = this.userProps.getNext();
			if( jprop.getName().equals(propName) ){
				return true;
				}
		}
		return false;
		
	}

	
	



	/**
	 * @return Returns the groupID.
	 */
	public JimetGroups getUserGroups() {
		return userGroups;
	}

	/**
	 * @param groupID The groupID to set.
	 */


	public void setUserGroups(JimetGroups userGroups) {
		this.userGroups = userGroups;
	}
	
	




	/**
	 * @return Returns the userProps.
	 */
	public JimetUserProperties getUserProps() {
		return userProps;
	}



	/**
	 * @param userProps The userProps to set.
	 */
	public void setUserProps(JimetUserProperties userProps) {
		this.userProps = userProps;
	}


	public int getId()
	{
		return this.getUserId();
	}

	public void setId(int id)
	{
		this.setUserId(id);
	}



 



}
